using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WPCoco.Graphics
{
    public class SimpleWaveNode : TextureNode
    {
        public SimpleWaveNode(AffineNode target, bool autoSize, Size gridSize)
            : base(target, autoSize)
        {
            grid = new VertexPositionColorTextureGrid(gridSize, new Size((int)Width, (int)Height));
            NeedUpdate = true;
        }

        public SimpleWaveNode(AffineNode target, Size gridSize)
            : this(target, true, gridSize)
        {

        }

        public SimpleWaveNode(AffineNode target)
            : this(target, new Size(10, 10))
        {

        }

        VertexPositionColorTextureGrid grid;

        Size gridSize;

        public Size GridSize
        {
            get { return gridSize; }
            set 
            {
                if (gridSize == value) return;
                gridSize = value;
                grid.SetGridSize(value);
            }
        }

        float waveSpeed = 1;

        public float WaveSpeed
        {
            get { return waveSpeed; }
            set { waveSpeed = value; }
        }

        float waveLength = 15;

        public float WaveLength
        {
            get { return waveLength; }
            set { waveLength = value; }
        }


        Vector2 waveCenter = Vector2.Zero;

        public Vector2 WaveCenter
        {
            get { return waveCenter; }
            set { waveCenter = value; }
        }

        float waveRadius = 100;

        public float WaveRadius
        {
            get { return waveRadius; }
            set { waveRadius = value; }
        }

        private float waveFactor = 0.2f;

        public float WaveFactor
        {
            get { return waveFactor; }
            set { waveFactor = value; }
        }

        float phase;

        public override void DrawTexture(Texture2D texture, Size size)
        {
            grid.SetUsedTextureSize(size);
            grid.SetTextureSize(new Size((int)texture.Width, (int)texture.Height));
            grid.SingleWaveGridState(waveCenter, waveRadius, waveFactor, waveLength, phase);
            grid.Draw(texture, WorldMatrix.Shared.CurrentMatrix);
        }

        protected override void SelfUpdate(GameTime gameTime)
        {
            phase += (float)gameTime.ElapsedGameTime.TotalSeconds * waveSpeed;
        }
    }
}
